package com.example.abe.diandi.trends.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.BindView;
import com.example.OnClick;
import com.example.abe.diandi.R;
import com.example.abe.diandi.trends.model.ImageTextModel;

import example.abe.com.framework.imageloader.ImageLoader;
import example.abe.com.framework.viewinject.ViewInjectUtils;

public class TrendsShowImageView extends FrameLayout {

    @BindView(R.id.view_trend_show_image_iv_photo)
    protected ImageView mIvPhoto;

    @BindView(R.id.view_trend_show_image_iv_photo_et_content)
    protected TextView mTvContent;

    private Context mContext;

    private ImageTextModel mImageTextModel;

    private OnImageViewClickListener mListener;

    public TrendsShowImageView(Context context, String id) {
        super(context);
        init(context, id);
    }

    private void init(Context context, String id) {
        mContext = context;
        View root = LayoutInflater.from(mContext).inflate(R.layout.view_trends_show_image, this, false);
        ViewInjectUtils.inject(this, root);
        addView(root);

        mImageTextModel = new ImageTextModel();
        mImageTextModel.setId(id);
    }

    public void setImageByNetwork(String path) {
        mImageTextModel.setImage(path);
        ImageLoader.getInstance().getImage(mIvPhoto, path);
    }

    public void setContent(String content) {
        mImageTextModel.setContent(content);
        mTvContent.setText(content);
        if (TextUtils.isEmpty(content)) {
            mTvContent.setVisibility(GONE);
        }
    }

    public void setTextContentVisibility(boolean isVisibility) {
        if (isVisibility) {
            mTvContent.setVisibility(VISIBLE);
        } else {
            mTvContent.setVisibility(GONE);
        }
    }

    public void setListener(OnImageViewClickListener listener) {
        mListener = listener;
    }

    public interface OnImageViewClickListener {
        void onImageViewClick(ImageView iv, ImageTextModel model);
    }

    @OnClick(R.id.view_trend_show_image_iv_photo)
    public void onImageViewClick(View view){
        if (mListener != null){
            mListener.onImageViewClick((ImageView)view, mImageTextModel);
        }
    }

}